package com.laicunba.event.listener;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.UserCreateEvent;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.TemplateDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.MobileMessageService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.TemplateService;
import com.laicunba.service.UserService;
import com.laicunba.service.exception.MyException;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

@Component
public class InviteRegistGrantCouponeListener implements ApplicationListener<UserCreateEvent> {
	private static final Logger LOG = Logger.getLogger(InviteRegistGrantCouponeListener.class);
	@Autowired
	private RegistryService registryService;
	@Autowired
	private CashCouponDefService cashCouponDefService;
	@Autowired
	private UserService userService;
	@Autowired
	private TemplateService templateService;
	@Autowired
	private MobileMessageService mobileMessageService;

	@Override
	public void onApplicationEvent(UserCreateEvent event) {
		if (event.getSource() == null) {
			return;
		}
		Map<String, String> map = registryService.likeKey("sys.invite.regist.cashcoupon.");
		if (!"true".equalsIgnoreCase(map.get("sys.invite.regist.cashcoupon.open"))) {
			return;
		}

		Date openDate = null;
		CashCouponDefDO defDO = null;
		RegistryDO registry = registryService.get(RegistryDO.class, "invite-info.201707.open");
		if(registry != null &&  StringUtil.isNotEmpty(registry.getVal())) {
			openDate = DateUtil.parse(registry.getVal());
			if(openDate != null && openDate.before(new Date())) {
				defDO = cashCouponDefService.get(CashCouponDefDO.class, "friend-regist");
			} else {
				LOG.warn("未开通了注册送现金券功能");
				return;
			}
		} else {
			LOG.warn("未开通了注册送现金券功能");
			return;
		}
		if (defDO == null) {
			LOG.warn("开通了注册送现金券功能,但是却找不到现金券模版");
			return;
		}
		try {
			UserDO user = (UserDO) event.getSource();
			if (StringUtil.isEmpty(user.getInviteFrom())) {
				return;
			}
			// 当前渠道不发送现金券
			if (user.getChannel() != null && user.getChannel().startsWith("no-award")) {
				return;
			}
			
			UserDO inviteUser = userService.findInviteUser(user.getId());
			if (inviteUser == null) {
				LOG.warn("邀请码:" + user.getInviteFrom() + ",找不到相应的用户");
				return;
			}
			cashCouponDefService.send2UserWithMessage(defDO.getId(), inviteUser.getId(), false, "邀请好友注册活动");

			TemplateDO templateDO = templateService.findByCodeWidthDefault("MOBILE_MSG_INVITE_REGIST_CASHCOUPON");
			Map<String, Object> paremeter = new HashMap<String, Object>();
			paremeter.put("MobileShortNo", StringUtil.endSubstring(user.getMobile(), 4));
			paremeter.put("cashCouponName", defDO.getAmount().toString() + "元现金券");
			String content = templateService.simpleMerge(templateDO.getContent(), paremeter);
			mobileMessageService.sendMsgAsync(inviteUser.getMobile(), content);
		} catch (MyException e) {
			LOG.error("注册送现金券的时候出错了", e);
		}

	}

}
